using System.Linq;
using UnityEngine;
using QFramework;
using UnityEngine.UI;

namespace ProjectMyFarmAndShe
{
	public partial class LevelUpPanel : ViewController,IController
	{
		private ILevelUpSystem mSystem;
		private LevelUpItemData mCurrentData;
		
		void Start()
		{
			mSystem = this.GetSystem<ILevelUpSystem>();
			
			// Code Here
			LevelItemTemplate.Hide();

			ActionKit.NextFrame(() =>
				{
					UpdateView();
				})
				.Start(this);
			
			BtnSubmit.onClick.AddListener(() =>
			{
				mCurrentData.OnFinish();
				UpdateView();
			});
		}

		void UpdateView()
		{
			Root.DestroyChildren();
			
			mCurrentData = mSystem.LevelUpItemDatas.FirstOrDefault(data => data.mUnlock() && !data.IsUp);
			if (mCurrentData != null)
			{
				LevelText.text = mCurrentData.Level.ToString();
				foreach (var itemData in mCurrentData.LevelItemDatas)
				{
					LevelItemTemplate.InstantiateWithParent(Root)
						.Self(self =>
						{
							self.Init(itemData);
						})
						.Show();
				}
			}
			else
			{
				BtnSubmit.interactable = false;
				BtnSubmit.GetComponentInChildren<Text>().text = "已完成";
			}
			
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}
